<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="awesome.Manager.ItemManager" %>
<%@ page import="awesome.Manager.Item" %>
<%@ page import="awesome.Manager.UserManager" %>
<%@ page import="awesome.Manager.User" %>
<%@ page import="awesome.Manager.PictureManager" %>
<%@ page import="awesome.Manager.Picture" %>
<%@ page import="awesome.Manager.BidManager" %>
<%@ include file='templates/header.jsp'%>
<%@include file='checkLoggedIn.jsp'%>
<%
 	int uid = Integer.valueOf(session.getAttribute("uid").toString());
	int iid = Integer.valueOf(request.getParameter("iid"));
	System.out.println(iid);
	ItemManager im = new ItemManager();
	UserManager um = new UserManager();
	PictureManager pm = new PictureManager();
	BidManager bm = new BidManager();
	
	Item item = im.getItem(iid);
	int ownerId = item.getUserID();
	User owner = um.getUser(ownerId);
	
	String picLocation = pm.getPicLoc(iid);
	System.out.println("loc: " + picLocation);
	String firstName = "";
	String lastName = "";
	
	if(owner != null){
		firstName = owner.getFirstName();
		lastName = owner.getLastName();
	}
	
	String function = request.getParameter("function");
	
	if(function != null){		
		if(function.equals("rate")){
			Double rating = Double.valueOf(request.getParameter("rate"));
			um.rateUser(ownerId,rating.floatValue());
		}else if(function.equals("bid")){
			Double amount = Double.valueOf(request.getParameter("amount"));
			bm.submitBid(uid,iid,amount.floatValue());
		}
	}
%>
<table cellspacing="10">
	<tr align="left">
		<td>
			<h2><%=item.getName()%></h2>
			<h3>Item Picture:</h3>
			<img src="<%=picLocation%>"/>
		</td>
	</tr>
	<tr>	
		<td>
			<h3>Owner: <%=firstName%> <%=lastName%></h3>
			<div> 
			<div>Current Rating: <%=um.getUserRating(ownerId) %></div>
			rate this user:
			<form action="view_item.jsp">
				<input type="hidden" name="iid" value="<%=iid %>"/>
				<input type="hidden" name="function" value="rate"/>
				0<input type="radio" name="rate" value="0"/>
				1<input type="radio" name="rate" value="1"/>
				2<input type="radio" name="rate" value="2"/>
				3<input type="radio" name="rate" value="3"/>
				4<input type="radio" name="rate" value="4"/>
				5<input checked="checked" type="radio" name="rate" value="5"/>
				<input type="submit" value="submit">	
			</form>
			</div>
			<br/>
			<div>
				<h3>start time: <%=item.getStartTime() %></h3> 
				<h3>end time: <%=item.getEndTime() %></h3>
			</div>
		</td>
	</tr>
	<tr>
		<td>
			<h3>Item Description:</h3>
			<p style="border:1px solid blue; width:300px;"><%=item.getDescription()%></p>
		</td>
	</tr>
	<tr>	
		<td>
			<div>current bid:   $<%=bm.getCurrentBid(iid)%></div>
			<form action="view_item.jsp">
				<input type="hidden" name="iid" value="<%=iid %>"/>
				<input type="hidden" name="function" value="bid">
				place bid: $<input type="text" name="amount" value="0.0">
				<input type="submit" value="submit">
			</form>
		</td>
	</tr>
</table>
<%@include file='templates/footer.jsp'%>
